Repository navigation
fix(common-utils): preserve boolean grouping for mixed AND/OR searches - #3690
Tyagiquamar wants to merge 1 commit into
Conversation
Signed-off-by: Tyagiquamar <mohdquamartyagi@gmail.com>
|
@Tyagiquamar is attempting to deploy a commit to the HyperDX Team on Vercel. A member of the Team first needs to authorize it. |
🦋 Changeset detectedLatest commit: 120b693 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| // AND before OR, regrouping `a AND b OR c` as `(a AND b) OR c`. Only wrap a | ||
| // child whose operator differs; same-operator, start-marked and negated | ||
| // forms keep their current spelling so saved searches are unaffected. | ||
| const negatingOperator = |
There was a problem hiding this comment.
🟠 major — Mixed chains with negated operators are still regrouped: a AND NOT b OR c keeps the bug this PR fixes
The lucene grammar parses AND NOT/OR NOT/NOT as single operators and right-nests them the same way it does AND/OR. So a AND NOT b OR c parses as AND NOT(a, OR(b, c)). Because of the !negatingOperator exclusion, the serializer still emits a AND NOT b OR c, which ClickHouse evaluates as (a AND NOT b) OR c. That is wrong: it should be a AND NOT (b OR c). Results for this query are still silently wrong, and it now behaves differently from a AND b OR c, which the PR does group. Fix: drop the negatingOperator exclusion so the OR operand is wrapped (NOT (b OR c)), or narrow it to a case the AST actually needs. Add tests for a AND NOT b OR c, a NOT b OR c and a OR NOT b AND c alongside the new mixed operator precedence cases. The current tests only cover AND/OR, so they would pass with this regression in place.
Nothing blocks merge automatically, but a maintainer will expect this fixed if it is a real defect in code this PR changes. If it is about surrounding code, reply and say so instead of patching. Do not widen the PR. How to respond
PR Review
1 finding(s): 🔴 0 critical · 🟠 1 major · 🔵 0 minor 1 posted as inline comment(s) on the changed lines. Severity is the reviewer's own estimate and is used for ordering, not filtering. No finding blocks merge automatically; a maintainer decides. |
Deep Review✅ No critical issues found. This is a tightly-scoped correctness fix to the lucene→SQL serializer in 🟡 P2 — recommended
🔵 P3 nitpicks (4)
Reviewers (6): testing, maintainability, project-standards, agent-native, learnings, previous-comments. Testing gaps:
No finding blocks merge automatically. A maintainer will expect P0/P1 findings in code this PR changes to be fixed; P2/P3 are your call -- fix or reply. Never fix findings about surrounding code here; reply instead. Do not widen the PR. How to respond |
Fixes #3032
Summary
A search that mixes
ANDandORwithout explicit parentheses now emits SQL that preserves the parse tree.a:1 AND b:2 OR c:3previously generated((a = '1') AND (b = '2') OR (c = '3')), which ClickHouse evaluates as(a AND b) OR c. It now generates((a = '1') AND ((b = '2') OR (c = '3'))), matching the parser'sa AND (b OR c)grouping.Continues #3032 after the baseline coverage in #3131, as requested by the maintainer.
Root Cause
serialize()inpackages/common-utils/src/queryParser.tsadded parentheses only when a node was markedparenthesizedin the source. The lucene parser right-nests mixed-operator chains, soa:1 AND b:2 OR c:3produces anANDnode whose right child is an unparenthesizedORnode. The serializer flattened that child. SQL bindsANDtighter thanOR, so ClickHouse regrouped the predicate and returned rows that should have been excluded, silently.Every log, trace, and dashboard search that mixed operators without writing parentheses was affected.
Solution
In the binary-AST branch of
serialize(), an operand is now wrapped in parentheses when it is a binary node with a different operator and no parentheses of its own. Same-operator chains, nodes carrying astart(NOT) marker, and negating parent operators keep their existing spelling, so saved searches that do not mix operators are unchanged. No APIs or fields were added.Added a patch changeset for
@hyperdx/common-utils.Regression test
Added
CustomSchemaSQLSerializerV2 - mixed operator precedencetopackages/common-utils/src/__tests__/queryParser.test.ts:ORgrouped;ANDchain is not over-parenthesized.On unmodified
main, the first two tests fail with((a = '1') AND (b = '2') OR (c = '3')); they pass with this change.How to reproduce
Build a
SearchQueryBuilderfora:"1" AND b:"2" OR c:"3"with String columnsa,b,c(as in the new test). Onmainthe innerORis ungrouped. On this branch it is grouped.Test plan
Validation on native Windows, Node v24.22.1 / Yarn 4.13.0 (CI uses the
.nvmrcNode 22.23.4; full-suite confirmation is left to GitHub Actions):npx prettier --checkon the two changed TypeScript files and the changeset: passed.npx eslint src/queryParser.ts src/__tests__/queryParser.test.ts --max-warnings 80(repo threshold): 0 errors, 18 pre-existing warnings in untouched lines: passed.yarn workspace @hyperdx/common-utils tsc --noEmit: passed.yarn workspace @hyperdx/common-utils jest src/__tests__/queryParser.test.ts --ci --coverage=false --runInBand --silent --verbose=false: 406/406 passed.